<template>
  <div class="channel-edit">
    <!-- 我的频道 -->
    <van-cell :border="false">
      <div class="titel-text" slot="title">我的频道</div>
      <van-button
        @click="isEdit=!isEdit"
        class="edit-btn"
        plain
        round
        size="mini"
        type="danger"
      >{{isEdit ? '完成' : '编辑'}}</van-button>
    </van-cell>

    <van-grid :gutter="10" class="my-grid">
      <van-grid-item
        :key="index"
        @click="onMyChannelClick(channel,index)"
        class="grid-item"
        v-for="channel,index in myChannels"
      >
        <van-icon
          @click="removeMyChannel(index)"
          name="cross"
          slot="icon"
          v-show="isEdit && !fixedChannel.includes(channel.id)"
        />
        <span :class="{active: index === active}" class="text" slot="text">{{channel.name}}</span>
      </van-grid-item>
    </van-grid>

    <!--频到推荐  -->
    <van-cell :border="false">
      <div class="titel-text" slot="title">频道推荐</div>
    </van-cell>

    <van-grid :gutter="10" class="recommend-grid">
      <van-grid-item
        :key="index"
        :text="recommendChannel.name"
        @click="onAddChannel(recommendChannel)"
        class="grid-item"
        icon="plus"
        v-for="recommendChannel,index in recommendChannels"
      />
    </van-grid>
  </div>
</template>

<script>
import { getAllChannels, addUserChannel, deleteUserChannel } from '@/api/channel'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage'

export default {
  name: "ChannelEdit",
  props: {
    myChannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      isEdit: false,
      allchannels: [],
      fixedChannel: [0]
    }
  },
  created () {
    this.loadAllChannels()
  },
  computed: {
    ...mapState(['user']),
    recommendChannels () {
      return this.allchannels.filter(channel => {
        return !this.myChannels.find(myChannel => myChannel.id === channel.id)
      })
    }
    // recommendChannels () {
    //   const channels = []
    //   this.allchannels.forEach(channel => {
    //     const ret = this.myChannels.find(myChannel => {
    //       return myChannel.id === channel.id
    //     })
    //     if (!ret) {
    //       channels.push(channel)
    //     }
    //   })
    //   return channels
    // }
  },
  methods: {
    async loadAllChannels () {
      try {
        const { data: res } = await getAllChannels()
        console.log(res.data)
        this.allchannels = res.data.channels
      } catch (err) {
        this.$toast('数据获取失败')
      }
    },
    async onAddChannel (channel) {
      this.myChannels.push(channel)
      // 数据持久化处理
      if (this.user) {
        // 已登录 把数据请求接口放到线上
        try {
          await addUserChannel({
            id: channel.id,
            seq: this.myChannels.length
          })
          console.log('chengg');
        } catch (err) {
          this.$toast('添加失败,稍后再试')
        }
      } else {
        // 未登录  把数据存储到本地
        setItem('TOUTIAO_CHANNELS', this.myChannels)
      }
    },
    onMyChannelClick (channel, index) {
      if (this.isEdit) {
        // 如果是固定频道  不能删除
        if (this.fixedChannel.includes(channel.id)) {
          return
        }
        // 删除频道项
        this.myChannels.splice(index, 1)
        // 删除模式
        if (index <= this.active) {
          this.$emit('update-active', this.active - 1, true)
        }

        // 处理持久化
        this.deleteChannel(channel)
      } else {
        // 跳转
        this.$emit('update-active', index, false)

      }
    },
    async deleteChannel (channel) {
      try {
        if (this.user) {
          await deleteUserChannel(channel.id)
        } else {
          // 未登录
          setItem('TOUTIAO_CHANNELS', this.myChannels)
        }
      } catch (err) {
        this.$toast('操作失败')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.channel-edit {
  padding: 85px 0;
  .titel-text {
    font-size: 32px;
    color: #333;
  }
  .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }
  .recommend-grid {
    /deep/ .grid-item {
      width: 160px;
      height: 86px;
      .van-grid-item__content {
        background-color: #f4f5f6;
        flex-direction: row;
        white-space: nowrap;
        .van-grid-item__text {
          font-size: 28px;
          color: #222;
          margin-top: 0;
        }
        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
      }
    }
  }
  /deep/ .my-grid {
    .van-grid-item__content {
      background-color: #f4f5f6;
    }
    .van-icon-cross {
      position: absolute;
      top: -10px;
      right: -10px;
      font-size: 15px;
      padding: 5px;
      color: #666;
      border: 1px solid #666;
      border-radius: 50%;
      background-color: #fff;
      z-index: 2;
    }
    .van-grid-item__text,
    .text {
      margin-top: 0;
      font-size: 28px;
      color: #222;
    }
    .active {
      color: red;
    }
    .van-grid-item__icon-wrapper {
      position: unset;
    }
  }
}
</style>